package org.atguigu.gmall.realtime.app;

import org.apache.flink.configuration.Configuration;
import org.apache.flink.runtime.state.hashmap.HashMapStateBackend;
import org.apache.flink.streaming.api.CheckpointingMode;
import org.apache.flink.streaming.api.environment.CheckpointConfig;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
import org.atguigu.gmall.realtime.common.Constant;
import org.atguigu.gmall.realtime.util.SQLUtil;

/**
 * @Author zenghaifeng
 * @Date 2023/4/24 20:42
 * @Package: org.atguigu.gmall.realtime.app
 * @Class: BaseSQLApp
 * @Description: TODO
 * @Version 1.0
 */
public abstract class BaseSQLApp {
    /**
     * @Author: zenghaifeng
     * @Date: 2023/4/24 20:43
     * @TODO: TODO initial environment.
     */
    public void init (int port,int parallelism,String name){
        System.setProperty("HADOOP_USER_NAME","atguigu");

        // get a stream execution environment
        Configuration conf = new Configuration();
        conf.setInteger("rest.port",port);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(parallelism);

        // set state backend
        env.setStateBackend(new HashMapStateBackend());

        // enable checkpoint
        env.enableCheckpointing(3000);
        // get checkpoint configuration
        CheckpointConfig checkpointConfig = env.getCheckpointConfig();
        // set checkpoint mode
        checkpointConfig.setCheckpointingMode(CheckpointingMode.EXACTLY_ONCE);
        // set checkpoint timeout ms 10s
        checkpointConfig.setCheckpointTimeout(10 * 1000);
        // set two checkpoint min interval ms
        checkpointConfig.setMinPauseBetweenCheckpoints(500);
        // set if delete checkpoint data when job is cancel
        checkpointConfig.setExternalizedCheckpointCleanup(CheckpointConfig.ExternalizedCheckpointCleanup.RETAIN_ON_CANCELLATION);

        // get a table execution environment
        StreamTableEnvironment tbe = StreamTableEnvironment.create(env);
        // set job name
        tbe.getConfig().getConfiguration().setString("pipeline.name",name);

        // need to implement handle
        handle(env,tbe);

    }

    protected abstract void handle(StreamExecutionEnvironment env,StreamTableEnvironment tbe);
    /**
     * @Author: zenghaifeng
     * @Date: 2023/4/24 21:16
     * @TODO: TODO read ods db and create a table in memory.
     * @param: tbe
     * @param: groupId
     */
    public void readOdsDb(StreamTableEnvironment tbe,String groupId){
        tbe.executeSql(" CREATE TABLE ods_db ( " +
                " `database` string, " +
                " `table` string, " +
                " `type` string, " +
                " `data` map<string,string>, " +
                " `old` map<string,string>, " +
                " `ts` bigint , " +
                " `pt` AS PROCTIME(), " +
                " `et` AS TO_TIMESTAMP_LTZ(ts,0), " +
                " WATERMARK FOR et AS et - INTERVAL '3' SECOND" +
                ")" + SQLUtil.getKafkaSourceDDL(Constant.TOPIC_ODS_DB,groupId));
    }

    public void readBaseDic(StreamTableEnvironment tbe){
        String sql =
                " CREATE TABLE `base_dic`( " +
                        " dic_code string, " +
                        "dic_name string " +
                " ) WITH (" +
                        " 'connector'='jdbc', " +
                        " 'url'='jdbc:mysql://hadoop162:3306/gmall2023?useSSL=false', " +
                        " 'table-name'='base_dic', " +
                        " 'username'='root', " +
                        " 'password'='aaaaaa', " +
                        " 'lookup.partial-cache.max-rows'='200', " +
                        " 'lookup.cache'='PARTIAL', " +
                        " 'lookup.partial-cache.expire-after-access'='20 second' " +
                        ") ";
        tbe.executeSql(sql);
    }
}
